package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.CartDao;
import dao.util.DBConnection;
import po.Cart;
import po.Client;
import po.Dish;

public class CartDaoImpl implements CartDao{
	Connection con = null;
	PreparedStatement ps = null;
	public int updateCart(String cdId,int num) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "update carts_dishes set num=? where cdId=?";
		int m = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, num);
			ps.setString(2, cdId);
			m = ps.executeUpdate();
			
			return m;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		return 0;
	}
	@Override//添加到cart表
	public int addCart(String cartId, int cId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "insert into c_carts(cartId,cId) values(?,?)";		
		int m = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, cartId);
			ps.setInt(2, cId);
			m = ps.executeUpdate();
			return m;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return 0;
	}
	@Override//删除cart表
	public int delCart(String cartId) {
		con = DBConnection.connect();
		ResultSet rs = null;
		String sql = "delete from c_carts where cartId=?";		
		int m = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, cartId);
			m = ps.executeUpdate();
			if(m>0){
				return 1;	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return 0;
	}
	@Override//查询cart表
	public List<String> queryCart(int cId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "select cartId from c_carts where cId=?";
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, cId);
			rs = ps.executeQuery();
			List<String> cartIdList = new ArrayList<String>();
			while(rs.next()){
				String cartId = rs.getString("cartId");
				cartIdList.add(cartId);
				
			}
			return cartIdList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return null;
	}
	@Override//查询cart_dish表
	public Cart queryCartDish(String cartId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "select * from carts_dishes where cartId = ?";
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, cartId);
			rs = ps.executeQuery();
			while(rs.next()){
				int dNum = rs.getInt("num");
				int dId = rs.getInt("dId");
				String cdId = rs.getString("cdId");
				Cart cart = new Cart(cartId, dNum, cdId, dId);
				return cart;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return null;
	}
	@Override//添加cart_dish表
	public int addCartDish(String cdId, String cartId, int num, int dId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "insert into carts_dishes(cdId,num,cartId,dId) values(?,?,?,?)";		
		int m = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, cdId);
			ps.setInt(2, num);
			ps.setString(3, cartId);
			ps.setInt(4, dId);
			m = ps.executeUpdate();
			return m;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return 0;
	}
	@Override//删除cart_dish表
	public int delCartDish(String cartId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "delete from carts_dishes where cartId=?";		
		int m = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, cartId);
			m = ps.executeUpdate();
			if(m>0){
				return 1;	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return 0;
	}

}
